package com.tcxr.web;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tcxr.framework.base.WebBase;
import com.tcxr.framework.system.Variables;
import com.tcxr.server.LoginServer;
import com.tcxr.validator.LoginViewValidator;
import com.tcxr.validator.RegisterViewValidator;
import com.tcxr.vo.LoginView;
@Controller
public class LoginWeb extends WebBase  {
	
	
	private static String REFERER="referer";
	
	@Resource  
    private LoginViewValidator loginViewValidator; 
	@Resource  
    private RegisterViewValidator registerViewValidator;
	
	@Autowired
	private LoginServer loginServer;
	
	
	
	
	
	
	
	/**
	 * 提交登陆信息
	 * @return
	 */
	@RequestMapping("/formLogin.do" )
	public String formLogin(
			ModelMap modelMap,
			@Valid @ModelAttribute  LoginView loginView,BindingResult result 
			)  
	{
		
		 
		this.loginViewValidator.validate(loginView, result);//添加验证  
		if (result.hasErrors()) {
			loginView.setLoPassword("");
			modelMap.addAttribute("loginView", loginView);// 把user对象返回到页面，
														  // 这样不至于表单被清空了
			return "/login/login";
		}else{
			
			
			//验证账号
		 String code=(String) super.getSession().getAttribute(SystemWeb.FORM_VERIFICATION_CODE);
				if ( !loginView.getVerify().equalsIgnoreCase(code) ||    loginView.getVerify().equals("") )
				{
					
					modelMap.addAttribute("verifyError", "验证码错误");
					return "/login/login";
				} else {
					if (!loginServer.login(loginView.getLoUser(),loginView.getLoPassword())) 
					{
						modelMap.addAttribute("loginError", "用户或密码错误");
						return "/login/login";
					}
				}
		}
	 //跳转到预存的URL中
		String url="redirect:/index";
	 String sessiontemp=(String) super.getSession().getAttribute(REFERER);
 
		if (sessiontemp == null) {
			url = "redirect:/index";
		} else {
			url = "redirect:" + sessiontemp;
		}
	
	 return url;
	 
	}
	
	
	/**
	 * 提交登陆信息
	 * @return
	 */
	@RequestMapping("/formRegister.do" )
	public String formRegiter(
			ModelMap modelMap,
			@Valid @ModelAttribute  LoginView loginView,BindingResult result 
			)
	{
		String url="redirect:/index";
		
				
		this.registerViewValidator.validate(loginView, result);//添加验证  
		if (result.hasErrors()) {
			loginView.setLoPassword("");
			loginView.setLoPasswordTwo("");
			modelMap.addAttribute("loginView", loginView);// 把user对象返回到页面，
														  // 这样不至于表单被清空了
			return "/login/register";
		}else {
			
			//验证账号
			 String code=(String) super.getSession().getAttribute(SystemWeb.FORM_VERIFICATION_CODE);
			if ( !loginView.getVerify().equalsIgnoreCase(code) || loginView.getVerify().equals("") )
			{
				modelMap.addAttribute("verifyError", "验证码错误");
				return "/login/register";
			}else{
				
			 int temp=loginServer.addLogin(loginView);
			 
			 	if(temp == -1)
			 	{
			 		modelMap.addAttribute("loginError", "用户名已被使用！");
			 		return "/login/register";
			 	}else if (temp == 0) {
			 		modelMap.addAttribute("loginError", "账号申请失败");
			 		return "/login/register";
				}else if (temp >0) {
					//账号申请成功,用账号登陆
					loginServer.login(loginView.getLoUser(), loginView.getLoPassword());
				}
				
			 	
			 	
			 	
			}
			
					
					
					
			
			
			
		}
		
		
		
		
		return "redirect:/index";
	}
	
	
	
	/**
	 * 注销用户
	 * @return
	 */
	 
	@RequestMapping("/cancellation")
	public String cancellation(ModelMap modelMap,HttpServletRequest request,HttpServletResponse response)
	{
		//记录跳转到登陆页面的url
		//super.getSession().setAttribute(REFERER, request.getHeader("Referer"));
		super.getSession().invalidate();
		//System.out.println("从某页面跳转过来的:"+request.getHeader("Referer"));
		//System.out.println(request.getHeader("Referer"));
		return "redirect:"+request.getHeader("Referer");
	}
	
	
	
	
	
	
	
	
	
	/**
	 * 跳到注册页面
	 * @return
	 */
	@RequestMapping("/register")
	public String toRegister()
	{
	//如果是已登陆的用户，就会屏蔽掉注册页面的跳转
		LoginView loginView=(LoginView) super.getSession().getAttribute(Variables.SESSION_USER);
		 if(loginView!=null){
		 return "redirect:/index";
		 }
		
		return "/login/register";
		
	}
	/**
	 * 跳到登陆页面
	 * @return
	 */
	 
	@RequestMapping("/login")
	public String toLogin(ModelMap modelMap,HttpServletRequest request,HttpServletResponse response)
	{
		//记录跳转到登陆页面的url
		super.getSession().setAttribute(REFERER, request.getHeader("Referer"));
		//  System.out.println(request.getHeader("Referer"));
		//System.out.println("从某页面跳转过来的:"+request.getHeader("Referer"));
		
		return "/login/login";
	}

}
//
//可以通过redirect/forward:url方式转到另一个Action进行连续的处理。
//
//可以通过redirect:url 防止表单重复提交 。
//
//写法如下：
//
//return "forward:/order/add";
//
//return "redirect:/index.jsp";
